package com.cupdata.zicon.dbesb.client;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.cupdata.zicon.dbesb.util.DateUtil;
import com.cupdata.zicon.dbesb.util.SignUtil_lj;
import junit.framework.TestCase;
import net.sf.json.JSONObject;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 联机交易接口
 */
public class OnlineTransDemo30 extends TestCase {
	static String uri = "https://access.credit2go.cn/escrowsr/p2p/online";
//	static String uri = "https://dev.credit2go.cn/srbank/p2p/online";
//	static String uri = "https://access.credit2go.cn/escrowsr/p2p/online";
//	static String uri = "https://test.credit2go.cn/escrowsr/p2p/online";
//	static String uri = "https://test.credit2go.cn/escrowsr/p2p/online";
//	static String uri = "https://access.credit2go.cn/escrow/p2p/online";
	//static String uri = "http://192.168.3.22:7080/escrow2/p2p/online";
    private static final String VERSION = "10";
    
    private static final String BANKCODE = "65264331";
	private static final String INSTCODE = "80098001";
	private static final String COINSTCHANNEL = "000001";
	private static final String SOURCE = "PX";
	private static final String PRODUCT = "0034";
	private static final String FUISSUER = "JM";

	@Test
    public void testDirectRechargePlus() throws Exception {
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "directRechargePlus");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "301133");
        reqMap.put("channel", COINSTCHANNEL);

        reqMap.put("accountId", "9999992002000002500");
        reqMap.put("idType", "1");
        reqMap.put("idNo", "310114198407200819");
        reqMap.put("name", "jinxin20");
        reqMap.put("exAcctPhone", "13800138020");
        reqMap.put("cardNo", "4385895587320");
        reqMap.put("txAmount", "12312312.12");
        reqMap.put("currency", "156");
        reqMap.put("userIP", "127.0.0.1");
        reqMap.put("notifyUrl", "http://www.baidu.com");
        reqMap.put("lastSrvAuthCode", "1098123353913293130");
        reqMap.put("smsCode", "111111");
        reqMap.put("password", "111111");
        reqMap.put("pwRandomNum", "000436C2B35D4B77BB44F8538630BFA1");




        testCommon(reqMap);
    }

	@Test
    public void testPwRandomNum() throws Exception {
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "pwRandomNum");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "301133");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("reqType", "1");
        testCommon(reqMap);
    }


	@Test
    public void testAccountOpen() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "accountOpenPlus");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "969894");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("idType","01");
        reqMap.put("idNo","310114198407200819");
        reqMap.put("name","jinxin20");
        reqMap.put("mobile","13800138020");
        reqMap.put("cardNo","4385895587320");
        reqMap.put("email","");     
        reqMap.put("acctUse","00000");   
        reqMap.put("lastSrvAuthCode","2039283996735450757");
//        reqMap.put("exAcctPhone","13800138020");
        reqMap.put("smsCode","111111");
        reqMap.put("password","111111");
        reqMap.put("pwRandomNum","CD1209A421F64353B7180B6C92CC3E74");
        reqMap.put("acqRes","");
        testCommon(reqMap);
    }


    @Test
    public void testBalanceQuery() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("accountId", "9999992002000002500");
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("instCode", INSTCODE);
        reqMap.put("seqNo", "137714");
        reqMap.put("txCode", "balanceQuery");
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("version", "10");

        testCommon(reqMap);
    }


    @Test
    public void testAccountIdQuery() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "accountIdQuery");
        reqMap.put("instCode",INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "222333");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("idType","01");    
        reqMap.put("idNo","9999992002000002500");
        
        testCommon(reqMap);
    }
	
	@Test
    public void testAccountDetailsQuery() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "accountDetailsQuery");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime",  DateUtil.getTime());
        reqMap.put("seqNo", "123456");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","9999992002000002500");
        reqMap.put("startDate","20170704");
        reqMap.put("endDate","20170704");
        reqMap.put("type","0");
        reqMap.put("tranType","");
        reqMap.put("pageNum","1");
        reqMap.put("pageSize","20");         
        
        testCommon(reqMap);
    }
	
	@Test
    public void testCardBind() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "cardBind");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "969894");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461330000003408");
    	reqMap.put("idType","01");
    	reqMap.put("idNo","320621199406126384");
    	reqMap.put("name","xiaoy");
    	reqMap.put("mobile","18651314288");
    	reqMap.put("cardNo","6222988812340059");
    	reqMap.put("acqRes","");       
        
        testCommon(reqMap);
    }
	
	@Test
    public void testCardUnbind() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "cardUnbind");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100002");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461270000000279");
    	reqMap.put("idType","01");
    	reqMap.put("idNo","510105199108260778");
    	reqMap.put("name","杨科奇");
    	reqMap.put("mobile","13551271737");
    	reqMap.put("cardNo","6222020020202206574");
		reqMap.put("acqRes","");		      
        
        testCommon(reqMap);
    }
	
	@Test
    public void testBalanceFreeze() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "balanceFreeze");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461330000003275");   
    	reqMap.put("orderId","123459");     
    	reqMap.put("txAmount","10");    
    	reqMap.put("acqRes","");      
        
        testCommon(reqMap);
    }
	    
	@Test
    public void testBalanceUnfreeze() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "balanceUnfreeze");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461330000003275");   
    	reqMap.put("orderId","123450");   
    	reqMap.put("txAmount","10");  
    	reqMap.put("orgOrderId","123459");
    	reqMap.put("acqRes","");    
   
        testCommon(reqMap);
    }
	
	@Test
    public void testDebtRegister() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "debtRegister");
        reqMap.put("instCode", "00170001");
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "123456");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461270000410791");   
    	reqMap.put("productId","66733");
    	reqMap.put("productDesc","123");
    	reqMap.put("raiseDate","20160721");
    	reqMap.put("raiseEndDate","20160725");
    	reqMap.put("intType","0");
    	reqMap.put("intPayDay","");
    	reqMap.put("duration","10");
    	reqMap.put("txAmount","100");
    	reqMap.put("rate","10");
    	reqMap.put("txFee","10");
    	reqMap.put("bailAccountId","");
    	reqMap.put("bailFee","");
    	reqMap.put("acqRes","");    
   
        testCommon(reqMap);
    }
	
	
	@Test
    public void testDebtRegisterCancel() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "debtRegisterCancel");
        reqMap.put("instCode", "00340001");
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "333222");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461450000506364");   
    	reqMap.put("productId","666665");
    	reqMap.put("raiseDate","20160721");
    	reqMap.put("acqRes","");    
   
        testCommon(reqMap);
    }

	@Test
    public void testAutoBidAuth() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "autoBidAuth");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461170000000362");   
        reqMap.put("orderId","123456");
        reqMap.put("txAmount","60");
        reqMap.put("totAmount","100");
        reqMap.put("retUrl","");
        reqMap.put("notifyUrl","");
    	reqMap.put("acqRes","");    
   
        testCommon(reqMap);
    }
	
	@Test
    public void testAutoBidAuthCancel() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "autoBidAuthCancel");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461170000000362");   
        reqMap.put("orderId","");
        reqMap.put("txAmount","");
        reqMap.put("txFee","");
        reqMap.put("productId","");
        reqMap.put("frzFlag","");
        reqMap.put("contOrderId","");
        reqMap.put("acqRes","");
   
        testCommon(reqMap);
    }
	
	@Test
    public void testBidCancel() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "bidCancel");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461170000000362");   
        reqMap.put("orderId","");
        reqMap.put("txAmount","");
        reqMap.put("txFee","");
        reqMap.put("productId","");
        reqMap.put("orgOrderId","");
        reqMap.put("acqRes","");
   
        testCommon(reqMap);
    }
	
	@Test
    public void testLendPay() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "lendPay");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "444222");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461330000003085");   
    	reqMap.put("orderId","12345678912345678912345678915");
    	reqMap.put("txAmount","100");
    	reqMap.put("bidFee","");
    	reqMap.put("debtFee","");
    	reqMap.put("bailFee","");
    	reqMap.put("forAccountId","6212461330000003101");
    	reqMap.put("productId","444222");
    	reqMap.put("authCode","20150917112619874571");
    	reqMap.put("acqRes","");
   
        testCommon(reqMap);
    }
	
	@Test
    public void testRepay() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "repay");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "969222");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461330000003101");   
    	reqMap.put("orderId","123456789123456789123678973");
    	reqMap.put("txAmount","10");
    	reqMap.put("intAmount","1.22");
    	reqMap.put("txFeeOut","");
    //	reqMap.put("txBailFee","");
    	reqMap.put("txFeeIn","");
    	reqMap.put("forAccountId","6212461330000003085");
    	reqMap.put("productId","969222");
    	reqMap.put("authCode","20150915144723870962");
    	reqMap.put("freezeFlag","1");
    	reqMap.put("acqRes","");
   
        testCommon(reqMap);
    }
	
	@Test
    public void testBidAutoApply() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "bidAutoApply");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461170000000362");   
        reqMap.put("orderId","");
    	reqMap.put("txAmount","");
    	reqMap.put("txFee","");
    	reqMap.put("productId","");
    	reqMap.put("frzFlag","");
    	reqMap.put("contOrderId","");
    	reqMap.put("acqRes","");
   
        testCommon(reqMap);
    }
	
	/**
	 * 自动债权转让
	 * @throws Exception
	 */
	@Test
    public void testCreditAutoInvest() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "creditAutoInvest");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461170000000362");   
        reqMap.put("orderId","");
    	reqMap.put("txAmount","");
    	reqMap.put("txFee","");
    	reqMap.put("tsfAmount","");
    	reqMap.put("forAccountId","");
    	reqMap.put("orgOrderId","");
    	reqMap.put("orgTxAmount","");
    	reqMap.put("productId","");
    	reqMap.put("contOrderId","");
    	reqMap.put("acqRes","");
   
        testCommon(reqMap);
    }
	
	/**
	 * 红包转账
	 * @throws Exception
	 */
	@Test
    public void testVoucherPay() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "voucherPay");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "777222");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461330000000370");   
    	reqMap.put("txAmount","10");
    	reqMap.put("forAccountId","6212461330000003085");
    	reqMap.put("desLineFlag","0");
    	reqMap.put("desLine","");
    	reqMap.put("acqRes","");
   
        testCommon(reqMap);
    }
	
	/**
	 * 红包转账撤销
	 * @throws Exception
	 */
	@Test
    public void testVoucherPayCancel() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "voucherPayCancel");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "555222");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461330000000370");   
        reqMap.put("txAmount","10");
        reqMap.put("forAccountId","6212461330000003085");
        reqMap.put("orgTxDate","20160719");
        reqMap.put("orgTxTime","161006");
        reqMap.put("orgSeqNo","666222");
        reqMap.put("acqRes","");
   
        testCommon(reqMap);
    }
	
	/**
	 * 惠财发短信
	 */
	@Test
	public void testSmsCodeApply() throws Exception{
		//bankcode=‘65264331’
		//instCode=‘80038001’
		 Map<String, Object> reqMap =new TreeMap<>();
	        reqMap.put("version", "10");
	        reqMap.put("txCode", "smsCodeApply");
	        reqMap.put("instCode", "80028001");
	        reqMap.put("bankCode", "65264331");
	        reqMap.put("txDate", DateUtil.getDate());
	        reqMap.put("txTime", DateUtil.getTime());
	        reqMap.put("seqNo", "555222");
	        reqMap.put("channel", COINSTCHANNEL);
	        reqMap.put("mobile", "13303971874");
	        reqMap.put("reqType", "1");
	        reqMap.put("srvTxCode", "accountOpenPlus");
	        //reqMap.put("cardNo", "6228480240516200013");
	        
	        testCommon(reqMap);
		
	}
	
	/**
	 * 惠财开户
	 */
	@Test
	public void testAccountOpenPlus() throws Exception{
		//bankcode=‘65264331’
		//instCode=‘80038001’
		 Map<String, Object> reqMap =new TreeMap<>();
	        reqMap.put("version", "10");
	        reqMap.put("txCode", "accountOpenPlus");
	        reqMap.put("instCode", "80048001");
	        reqMap.put("bankCode", "65264331");
	        reqMap.put("txDate", DateUtil.getDate());
	        reqMap.put("txTime", DateUtil.getTime());
	        reqMap.put("seqNo", "555222");
	        reqMap.put("channel", COINSTCHANNEL);
	        reqMap.put("idType", "01");
	        reqMap.put("idNo", "611022198904218010");
	        reqMap.put("name", "李伦昊");
	        reqMap.put("mobile", "13303971874");
	        reqMap.put("cardNo", "6212260240516200014");
	        reqMap.put("acctUse", "00000");
	        reqMap.put("lastSrvAuthCode", "34619208175036885");
	        reqMap.put("smsCode", "111111");
	        reqMap.put("password", "123456");

	        
	        testCommon(reqMap);
		
	}
	
	/**
	 * 结束债权
	 * @throws Exception
	 */
	
	@Test
    public void testBidApplyQuery() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "bidApplyQuery");
        reqMap.put("instCode", "00590001");
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "969894");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461630000000225");   
        reqMap.put("orgOrderId","JTZEZ1CM6A9Y6DJHIEY8KODRSIJL4C");
        reqMap.put("acqRes","");
   
        testCommon(reqMap);
    }
	
	/**
	 * 结束债权
	 * @throws Exception
	 */

	@Test
	
    public void testCreditEnd() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "creditEnd");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461170000000362");   
        reqMap.put("orderId","");
        reqMap.put("forAccountId","");
        reqMap.put("productId","");
        reqMap.put("authCode","");
        reqMap.put("acqRes","");
        testCommon(reqMap);
    }
	
	/**
	 * 投资人债权明细查询
	 * @throws Exception
	 */
	
	@Test
    public void testcreditDetailsQuery() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "creditDetailsQuery");
        reqMap.put("instCode", "00320001");
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "222969");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461420000000494");   
        reqMap.put("productId","");
        reqMap.put("state","0");
        reqMap.put("startDate","20160820");
        reqMap.put("endDate","20160821");
        reqMap.put("pageNum","1");
        reqMap.put("pageSize","10");
        testCommon(reqMap);
    }
	
	/**
	 * 担保户代偿
	 * @throws Exception
	 */
	@Test
    public void testBailRepay() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "bailRepay");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461170000000362");   
        reqMap.put("orderId","");
    	reqMap.put("txAmount","");
    	reqMap.put("txCapAmout","");
    	reqMap.put("txIntAmount","");
    	reqMap.put("forAccountId","");
    	reqMap.put("orgOrderId","");
    	reqMap.put("orgTxAmount","");
    	reqMap.put("productId","");
    	reqMap.put("acqRes","");
        testCommon(reqMap);
    }
	
	/**
	 * 融资人还担保账户垫款
	 * @throws Exception
	 */
	@Test
    public void testRepayBail() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "repayBail");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461170000000362");   
        reqMap.put("orderId","");
        reqMap.put("txAmount","");
        reqMap.put("intAmount","");
        reqMap.put("txFeeOut","");
        reqMap.put("forAccountId","");
        reqMap.put("orgOrderId","");
        reqMap.put("authCode","");
        reqMap.put("freezeFlag","");
        reqMap.put("acqRes","");
        testCommon(reqMap);
    }
	
	/**
	 * 绑卡关系查询
	 * @throws Exception
	 */
	@Test
    public void testCardBindDetailsQuery() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "cardBindDetailsQuery");
        reqMap.put("instCode", "00340001");
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "222969");
        reqMap.put("channel", COINSTCHANNEL);
        reqMap.put("accountId","6212461450000099964");   

        testCommon(reqMap);
    }
	
	
	/**
	 * 借款人标的信息查询
	 * @throws Exception
	 */
	@Test
    public void testDebtDetailsQuery() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "debtDetailsQuery");
        reqMap.put("instCode", "00220001");
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "444222");
        reqMap.put("channel", COINSTCHANNEL);
        
        reqMap.put("accountId","123123123");   
        reqMap.put("productId","");
        reqMap.put("startDate","20150719");
        reqMap.put("endDate","20160725");
        reqMap.put("pageNum","1");
        reqMap.put("pageSize","30");

        testCommon(reqMap);
    }
	
	
	/**
	 * 电子账户手机号查询维护
	 * @throws Exception
	 */
	@Test
    public void testMobileMaintainace() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "mobileMaintainace");
        reqMap.put("instCode", "00220001");
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        
        reqMap.put("accountId","6212461450000099964");   
        reqMap.put("option","1");
        reqMap.put("mobile","18168898639");
        reqMap.put("acqRes","");

        testCommon(reqMap);
    }
	
	/**
	 * 按手机号查询电子账号信息
	 * @throws Exception
	 */
	@Test
    public void testAccountQueryByMobile() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "accountQueryByMobile");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "222969");
        reqMap.put("channel", COINSTCHANNEL);
        
        reqMap.put("mobile","18818276236");

        testCommon(reqMap);
    }
	
	/**
	 * 查询交易状态
	 * 功能说明：根据交易流水号或者订单号查询交易的状态（可以查询放款、还款交易）
	 * @throws Exception
	 */
	@Test
    public void testTransactionStatusQuery() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "transactionStatusQuery");
        reqMap.put("instCode", "00370001");
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        
        reqMap.put("accountId","6212461480000000068");
        reqMap.put("reqType","2");	
        reqMap.put("reqTxCode","repay");
        reqMap.put("reqTxDate","");
        reqMap.put("reqTxTime","");
        reqMap.put("reqSeqNo","");
        reqMap.put("reqOrderId","15514826245982");
        
        testCommon(reqMap);
    }
	
	/**
	 * 查询批次状态
	 * 功能说明：查询批次的状态，包括批次放款、批次还款、批次融资人还担保账户垫款接口、批次结束债权。
	 * @throws Exception
	 */
	@Test
    public void testBatchQuery() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "batchQuery");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        
        reqMap.put("batchTxDate","6212461170000000362");   
        reqMap.put("batchNo","");
        
        testCommon(reqMap);
    }
	
	/**
	 * 批次还款
	 * @throws Exception
	 */
	@Test
    public void testBatchLendPay() throws Exception{
		List<Map<String, String>> arrayList = new ArrayList<>();
		Map<String, String> map1 = new HashMap<>();
		map1.put("accountId","");
        map1.put("orderId","");
        map1.put("txAmount","");
        map1.put("bidFee","");
        map1.put("debtFee","");
        arrayList.add(map1);

        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "batchLendPay");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        
        reqMap.put("batchNo","");
        reqMap.put("txAmount","");
        reqMap.put("txCounts","");
        reqMap.put("notifyURL","");
        reqMap.put("retNotifyURL","");
        reqMap.put("acqRes","");
        reqMap.put("subPacks",arrayList);
        
        
        
        testCommon(reqMap);
    }
	
	/**
	 * 批次还款
	 * @throws Exception
	 */
	@Test
    public void testBatchRepay() throws Exception{
		List<Map<String, String>> arrayList = new ArrayList<>();
		Map<String, String> map1 = new HashMap<>();
		map1.put("accountId","");
        map1.put("orderId","");
        map1.put("forAccountId","");
        map1.put("productId","");
        map1.put("authCode","");
        arrayList.add(map1);

        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "batchRepay");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        
        reqMap.put("batchNo","");
        reqMap.put("txCounts","");
        reqMap.put("notifyURL","");
        reqMap.put("retNotifyURL","");
        reqMap.put("acqRes","");
        reqMap.put("subPacks",arrayList);
        
        
        testCommon(reqMap);
    }
	
	/**
	 * 批次结束债权
	 * @throws Exception
	 */
	@Test
    public void testBatchCreditEnd() throws Exception{
		List<Map<String, String>> arrayList = new ArrayList<>();
		Map<String, String> map1 = new HashMap<>();
		map1.put("accountId","");
        map1.put("orderId","");
        map1.put("forAccountId","");
        map1.put("productId","");
        map1.put("authCode","");

        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "batchCreditEnd");
        reqMap.put("instCode", INSTCODE);
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        
        reqMap.put("batchNo","");
        reqMap.put("txCounts","");
        reqMap.put("notifyURL","");
        reqMap.put("retNotifyURL","");
        reqMap.put("acqRes","");
        reqMap.put("subPacks",arrayList);
        
        
        testCommon(reqMap);
    }
	
	/*
	 * 查联行号
	 */
	@Test
	public void testLianhanghao() throws Exception{
		 Map<String, Object> reqMap =new TreeMap<>();
	        reqMap.put("version", "10");
	        reqMap.put("txCode", "payAllianceCodeQuery");
	        reqMap.put("instCode", "00170001");
	        reqMap.put("bankCode", BANKCODE);
	        reqMap.put("txDate", DateUtil.getDate());
	        reqMap.put("txTime", DateUtil.getTime());
	        reqMap.put("seqNo", "100101");
	        reqMap.put("channel", COINSTCHANNEL);
	        
	        reqMap.put("accountId","6228480240516200013");
	        testCommon(reqMap);
	}
	
	/**
	 * 款或还款撤销
	 * 功能说明：P2P平台在放款、还款、融资人还担保账户垫款未被集中处理前可以撤销该交易。
	 * @throws Exception
	 */
	@Test
	public void testPayCancel() throws Exception{

	    Map<String, Object> reqMap =new TreeMap<>();
	    reqMap.put("version", "10");
	    reqMap.put("txCode", "payCancel");
	    reqMap.put("instCode", INSTCODE);
	    reqMap.put("bankCode", BANKCODE);
	    reqMap.put("txDate", DateUtil.getDate());
	    reqMap.put("txTime", DateUtil.getTime());
	    reqMap.put("seqNo", "100101");
	    reqMap.put("channel", COINSTCHANNEL);
	    
	    reqMap.put("accountId","");
	    reqMap.put("txAmount","");
	    reqMap.put("forAccountId","");
	    reqMap.put("orgTxDate","");
	    reqMap.put("orgTxTime","");
	    reqMap.put("orgSeqNo","");
	    reqMap.put("orgTxCode","");
	    reqMap.put("acqRes","");
	    
	    
	    testCommon(reqMap);
	}

	
	/**
	 * 查询批次交易明细状态
	 * 功能说明：查询批次交易明细的状态，包括批次放款、批次还款、批次融资人还担保账户垫款接口、批次结束债权。
	 * @throws Exception
	 */
	@Test
    public void testBatchDetailsQuery() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "batchDetailsQuery");
        reqMap.put("instCode", "00340001");
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "222969");
        reqMap.put("channel", COINSTCHANNEL);
        
        reqMap.put("batchTxDate","20160928");
        reqMap.put("batchNo","100008");
        reqMap.put("type","0");
        reqMap.put("pageNum","1");
        reqMap.put("pageSize","30");

        testCommon(reqMap);
    }
	
	/**
	 * 查询单笔投资人购买债权
	 * @throws Exception
	 */
	@Test
    public void testCreditInvestQuery() throws Exception{
        Map<String, Object> reqMap =new TreeMap<>();
        reqMap.put("version", "10");
        reqMap.put("txCode", "creditInvestQuery");
        reqMap.put("instCode", "00170001");
        reqMap.put("bankCode", BANKCODE);
        reqMap.put("txDate", DateUtil.getDate());
        reqMap.put("txTime", DateUtil.getTime());
        reqMap.put("seqNo", "100101");
        reqMap.put("channel", COINSTCHANNEL);
        
        reqMap.put("accountId","");
    	reqMap.put("orgOrderId","");
    	reqMap.put("acqRes","");

        testCommon(reqMap);
    }
	@Test
	public void testCreditAuthQuery() throws Exception{
		 Map<String, Object> reqMap =new TreeMap<>();
	        reqMap.put("version", "10");
	        reqMap.put("txCode", "creditAuthQuery");
	        reqMap.put("instCode", "00170001");
	        reqMap.put("bankCode", BANKCODE);
	        reqMap.put("txDate", DateUtil.getDate());
	        reqMap.put("txTime", DateUtil.getTime());
	        reqMap.put("seqNo", "100101");
	        reqMap.put("channel", COINSTCHANNEL);
	        reqMap.put("accountId","6212461270000007555");
	        reqMap.put("acqRes","");
	        reqMap.put("type","1");

	        testCommon(reqMap);
	}
	
	
    public void testCommon(Map<String, Object> reqMap) throws Exception{
         RestTemplate restTemplate = new RestTemplate(new ArrayList<HttpMessageConverter<?>>(){{add(new FastJsonHttpMessageConverter());}});
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String requestMapMerged = mergeMap(reqMap);
        System.out.println("requestMapMerged:  "+ requestMapMerged);
		
		//处理签名
		String sign = SignUtil_lj.sign(requestMapMerged);
		System.out.println("签名:" + sign);
		reqMap.put("sign", sign);
        System.out.println("签名成功");

        headers.set("Accept-Charset", "UTF-8");
        headers.set("contentType","application/json");
        trustAllHttpsCertificates();
        HttpsURLConnection.setDefaultHostnameVerifier(hv);

        HttpEntity entity = new HttpEntity(reqMap,headers);

        System.out.println("发送请求至："+uri);

        ResponseEntity response  = restTemplate.exchange(uri,HttpMethod.POST, entity, Map.class);

      //响应报文
        System.out.println(response.getBody());
        Map responseMap = (Map)response.getBody();
        
        JSONObject  paramsJson = JSONObject.fromObject(responseMap);

        //验签
        String responseSign = (String) responseMap.get("sign");
        responseMap.remove("sign");
        
        String responseMapMerged = mergeMap(new TreeMap(responseMap));

        System.out.println("responseMerged: "+responseMapMerged.toString());

        boolean verifyResult = SignUtil_lj.verify(responseSign, responseMapMerged.toString());
        if (!verifyResult){
            System.out.println("验证签名失败...");
            return;
        } else {
            System.out.println("验证签名成功");
        }

        System.out.println("retCode:" + responseMap.get("retCode"));
        System.out.println("retMsg:"+responseMap.get("retMsg"));
        System.out.println("返回结果:"+responseMap.toString());
        assertEquals(verifyResult, true);
    }



	HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            System.out.println("Warning: URL Host: " + urlHostName + " vs. "
                    + session.getPeerHost());
            return true;
        }
    };

    private static void trustAllHttpsCertificates() throws Exception {
        javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
        javax.net.ssl.TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
                .getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc
                .getSocketFactory());
    }

public     static class miTM implements javax.net.ssl.TrustManager,
            javax.net.ssl.X509TrustManager {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public boolean isServerTrusted(
                java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public boolean isClientTrusted(
                java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }

    }
    
    public static String mergeMap(Map map ) {
    	Map reqMap = new TreeMap(map);
    	
		String requestMerged = "";
		StringBuffer buff = new StringBuffer();
		Iterator<Map.Entry<String, String>> iter = reqMap.entrySet().iterator();
		Map.Entry<String, String> entry;
		while (iter.hasNext()) {
			entry = iter.next();
			if (!"sign".equals(entry.getKey())) {
				if(entry.getValue()==null){
					entry.setValue("");
					buff.append("");
				}else{
					if("subPacks".equalsIgnoreCase(entry.getKey()) ){
						StringBuffer substr = null;
						if(reqMap.get("subPacks") != null){
							substr  = new StringBuffer();
				    		
				    		List<Map> submapList = (List<Map>)reqMap.get("subPacks");
							for(Map submap : submapList){
								substr.append(mergeMap(submap));
							}
						}
						buff.append(String.valueOf(substr.toString()));
					}else{
						buff.append(String.valueOf(entry.getValue()));
					}
				}
			}
		}
		requestMerged = buff.toString();
		return requestMerged;
	}
    }
